﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using Payroll;
using ListBox = System.Windows.Forms.ListBox;

namespace pay_mng
{
    public partial class PayMng : Form
    {
        private MySqlPayrollDatabase payrollDatabase = MySqlPayrollDatabase.Instance;
        private string title;
        public PayMng()
        {
            InitializeComponent();
            initData();
            title = this.Text;
        }

        private void initData()
        {
            this.employeeListView.Items.Clear();
            IList list = payrollDatabase.GetAllEmployees();
            this.employeeListView.BeginUpdate();
            foreach (var v in list)
            {
                Employee e = v as Employee;
                ListViewItem item = new ListViewItem(e.EmpId.ToString());
                item.SubItems.Add(e.Name);
                item.SubItems.Add(e.Address);
                item.SubItems.Add(e.Affiliation is UnionAffiliation ? "有" : "无");
                item.SubItems.Add(e.Classification is HourlyClassification
                    ? "临时工"
                    : (e.Classification is SalariedClassification ? "月工" : "月工(绩效)"));
                item.SubItems.Add(e.Method is DirectDepositMethod
                   ? "直接打款"
                   : (e.Classification is HoldMethod ? "支票" : "邮寄"));
                double baseRate = 0.0D;
                double commissionRate = 0.0D;
                int classification = e.Classification is HourlyClassification
                    ? 1
                    : (e.Affiliation is SalariedClassification ? 2 : 3);
                int method = e.Method is DirectDepositMethod ? 1 : (e.Method is HoldMethod ? 2 : 3);
                if (classification == 1)
                {
                    baseRate = (e.Classification as HourlyClassification).HourlyRate;
                }
                else if (classification == 2)
                {
                    baseRate = (e.Classification as SalariedClassification).Salary;
                }
                else
                {
                    baseRate = (e.Classification as CommissionClassification).BaseRate;
                    commissionRate = (e.Classification as CommissionClassification).CommissionRate;
                }
                item.SubItems.Add(baseRate.ToString());
                item.SubItems.Add(commissionRate.ToString());
                this.employeeListView.Items.Add(item);
            }
            this.employeeListView.EndUpdate();
            this.employeeListView.Focus();
        }

        private void employeeListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        // todo
        private void addEmployee_Click(object sender, EventArgs e)
        {
            
        }

        // todo
        private void deleteEmployeeBtn_Click(object sender, EventArgs e)
        {
            
        }

        // todo
        private void changeEmployeeBtn_Click(object sender, EventArgs e)
        {
            
        }

        // todo
        private void payBtn_Click(object sender, EventArgs e)
        {
            
        }

        // todo
        private void registerBtn_Click(object sender, EventArgs e)
        {
            
        }

        // todo
        private void saveStatusBtn_Click(object sender, EventArgs e)
        {
            

        }

        private void PayMng_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!payrollDatabase.Balking)
            {
                DialogResult result = MessageBox.Show("数据尚未保存，你确定要关闭吗！", "提示信息", MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Information);
                if (result == DialogResult.OK)
                {
                    e.Cancel = false; //点击OK   
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        private void AddMenuItem_Click(object sender, EventArgs e)
        {
            AddEmployeeForm form = new AddEmployeeForm(this.employeeListView, false, null);
            form.ShowDialog(this);
            if (!payrollDatabase.Balking)
            {
                this.Text = title + "*(未保存)";
            }
        }

        private void changeMenuItem_Click(object sender, EventArgs e)
        {
            ListViewItem selected = this.employeeListView.FocusedItem;
            if (selected == null)
            {
                MessageBox.Show("雇员不能为空", "错误");
                return;
            }
            int id = Convert.ToInt32(selected.SubItems[0].Text);

            Employee employee = payrollDatabase.GetEmployee(id);

            
            AddEmployeeForm form = new AddEmployeeForm(this.employeeListView, true, employee);

            // 设置模态窗口，组织子窗口事件向父窗口传播
            form.ShowDialog(this);
            if (!payrollDatabase.Balking)
            {
                this.Text = title + "*(未保存)";
            }
        }

        private void deleteMenuItem_Click(object sender, EventArgs e)
        {
            ListViewItem selected = this.employeeListView.FocusedItem;
            if (selected == null)
            {
                MessageBox.Show("雇员不能为空", "错误");
                return;
            }

            int id = Convert.ToInt32(selected.SubItems[0].Text);

            Employee employee = payrollDatabase.GetEmployee(id);

            DeleteEmployeeTransaction deleteEmployeeTx = new DeleteEmployeeTransaction(employee.EmpId, payrollDatabase);
            deleteEmployeeTx.Execute();

            this.initData();
        }

        private void registerMenuItem_Click(object sender, EventArgs e)
        {
            ListViewItem selected = this.employeeListView.FocusedItem;
            if (selected == null)
            {
                MessageBox.Show("雇员不能为空", "错误");
                return;
            }

            int id = Convert.ToInt32(selected.SubItems[0].Text);

            Employee employee = payrollDatabase.GetEmployee(id);
            RegisterForm registerForm = new RegisterForm(employee);
            registerForm.ShowDialog(this);
        }

        private void payMenuItem_Click(object sender, EventArgs e)
        {
            PayForm form = new PayForm();
            form.ShowDialog(this);
        }

        private void saveStatusMenuItem_Click(object sender, EventArgs e)
        {
            MySqlPayrollDatabase mySqlPayroll = MySqlPayrollDatabase.Instance;
            try
            {
                mySqlPayroll.SaveStatus();
                MessageBox.Show("保存成功", "通知");
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "错误");
                throw;
            }

            this.Text = title;
        }
    }
}
